<template>
  <div>
    <el-card>
      <el-switch
        style="display: block;width:50%;"
        v-model="draggable"
        active-color="#13ce66"
        inactive-color="rgb(192, 196, 204)"
        active-text="开启拖拽功能"
        inactive-text="关闭拖拽功能"
      >
      </el-switch>
      <el-button style="float: right;margin-top: -30px" @click="batchDelete" type="danger">批量删除</el-button>
      <el-button style="float: right;margin-top: -30px;margin-right:18px" @click="resetChecked">清空</el-button>
    </el-card>
    <br />

    <el-tree
      ref="menuTree"
      :data="menus"
      :props="defaultProps"
      @node-click="handleNodeClick"
      :expand-on-click-node="false"
      show-checkbox
      node-key="catId"
      :default-expanded-keys="expandedKey"
      :draggable="draggable"
      :allow-drop="allowDrop"
      @node-drop="handleDrop"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level <= 2"
            type="text"
            size="mini"
            @click="() => append(data)"
          >
            Append
          </el-button>
          <el-button type="text" size="mini" @click="() => edit(data)">
            Edit
          </el-button>
          <el-button
            v-if="node.childNodes.length === 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>
    <!-- 添加菜单对话框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <!-- <span>请输入菜单信息</span> -->
      <el-form :model="category" :rules="rules" ref="category">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input
            v-model="category.sort"
            type="number"
            maxLength="9"
            minlength="1"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类图标URL">
          <el-input v-model="category.icon" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="计量单位">
          <el-input
            v-model="category.productUnit"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="活动区域" :label-width="formLabelWidth">
      <el-select v-model="form.region" placeholder="请选择活动区域">
        <el-option label="区域一" value="shanghai"></el-option>
        <el-option label="区域二" value="beijing"></el-option>
      </el-select>
    </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          v-if="dialogType == 'add'"
          @click="addCategory"
          >提 交</el-button
        >
        <el-button
          type="primary"
          v-if="dialogType != 'add'"
          @click="editCategory"
          >保 存</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "RenrenFastVueCategory",
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      draggable: false,
      updateNodes: [],
      maxNum: 0,
      category: {
        catId: "",
        name: "",
        parentCid: 0,
        catLevel: 0,
        sort: 0,
        icon: "",
        productUnit: "",
        productCount: 0,
      },
      menus: [],
      expandedKey: [], //默认展开的数据
      defaultProps: {
        children: "children",
        label: "name",
      },
      dialogVisible: false, //对话框显示状态
      dialogType: "", //edit,add
      dialogTitle: "编辑菜单信息", //edit,add
      //表单验证规则
      rules: {
        name: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getMenus();
  },

  mounted() {},

  methods: {
    //清空
    resetChecked() {
        this.$refs.menuTree.setCheckedKeys([]);
      },
    //批量删除
    batchDelete() {
      let checkedNods = this.$refs.menuTree.getCheckedNodes();
      let catIds = [];
      console.log(checkedNods);
      if (checkedNods != undefined && checkedNods.length > 0) {
        for (let i = 0; i < checkedNods.length; i++) {
          catIds.push(checkedNods[i].catId);
        }
        this.$confirm(`确定要删除这些分类吗？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$http({
              url: this.$http.adornUrl("/product/category/delete"),
              method: "post",
              data: this.$http.adornData(catIds, false),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.getMenus();
                this.$message({
                  message: "分类删除成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {},
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          })
          .catch(() => {
            this.$message({
              message: "操作取消",
              type: "info",
              duration: 1500,
            });
          });
      }
    },
    //拖拽完成
    handleDrop(draggingNode, dropNode, dropType, ev) {
      console.log("handleDrop: ", draggingNode, dropNode, dropType, ev);
      //1.当前节点最新的父节点ID
      let pCid = 0;
      //2.当前拖拽节点的最新顺序
      let siblings = [];
      if (dropType == "before" || dropType == "after") {
        pCid =
          dropNode.parent.data.catId == undefined
            ? 0
            : dropNode.parent.data.catId;
        siblings = dropNode.parent.childNodes;
      } else {
        pCid = dropNode.data.catId;
        siblings = dropNode.childNodes;
      }

      for (let i = 0; i < siblings.length; i++) {
        //如果是当前拖拽的节点  会影响父子关系的ID
        if (siblings[i].data.catId == draggingNode.data.catId) {
          //如果当前节点层级发生了变化
          let catLevel = draggingNode.level;
          if (siblings[i].level != draggingNode.level) {
            //修改当前节点的层级变化  拿到最新层级
            catLevel = siblings[i].level;
            //修改他子节点的层级
            this.updateChildNodeLevel(siblings[i]);
          }
          this.updateNodes.push({
            catId: siblings[i].data.catId,
            sort: i,
            parentCid: pCid,
            catLevel: catLevel,
          });
        } else {
          this.updateNodes.push({ catId: siblings[i].data.catId, sort: i });
        }
      }

      //3.所在层级
      console.log(this.updateNodes);
      this.$http({
        url: this.$http.adornUrl("/product/category/update/sort"),
        method: "post",
        data: this.$http.adornData(this.updateNodes, false),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.getMenus();
          this.maxNum = 0;
          this.updateNodes = [];
          this.expandedKey = [pCid];
          this.$message({
            message: "菜单顺序修改成功",
            type: "success",
            duration: 1500,
            onClose: () => {},
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //拖拽功能 修改子节点层级 的方法
    updateChildNodeLevel(node) {
      if (node.childNodes.length > 0) {
        for (let i = 0; i < node.childNodes.length; i++) {
          var cNode = node.childNodes[i].data;
          this.updateNodes.push({
            catId: cNode.catId,
            catLevel: node.childNodes[i].level,
          });
          this.updateChildNodeLevel(node.childNodes[i]);
        }
      }
    },
    //每次点击的时候
    handleNodeClick(data) {
      console.log(data);
    },
    getMenus() {
      this.$http({
        url: this.$http.adornUrl("/product/category/list/tree"),
        methods: "get",
      }).then((data) => {
        console.log("成功获取到菜单信息", data.data.data);
        this.menus = data.data.data;
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    //添加菜单 点击保存
    addCategory() {
      this.$refs["category"].validate((valid) => {
        if (valid) {
          if (!Number.isInteger(this.category.sort * 1)) {
            this.category.sort = 0;
          }
          //发送请求
          this.$http({
            url: this.$http.adornUrl("/product/category/save"),
            method: "post",
            data: this.$http.adornData(this.category, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.getMenus();
              this.dialogVisible = false;
              this.expandedKey = [this.category.parentCid];
              this.$message({
                message: "菜单添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {},
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
      console.log(this.category);
    },
    //修改
    editCategory() {
      this.$refs["category"].validate((valid) => {
        if (valid) {
          if (!Number.isInteger(this.category.sort * 1)) {
            this.category.sort = 0;
          }
          var { catId, name, icon, sort, productCount } = this.category;
          //发送请求
          this.$http({
            url: this.$http.adornUrl("/product/category/update"),
            method: "post",
            data: this.$http.adornData(
              { catId, name, icon, sort, productCount },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.getMenus();
              this.dialogVisible = false;
              this.expandedKey = [this.category.parentCid];
              this.$message({
                message: "菜单修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {},
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
      console.log(this.category);
    },
    //点击添加时
    append(data) {
      //初始化数据
      this.dialogType = "edit";
      this.dialogTitle = "编辑菜单";
      this.category.name = "";
      this.category.catId = null;

      this.category.icon = "";
      this.category.sort = 0;
      this.category.productUnit = "";
      this.category.productCount = 0;

      this.dialogType = "add";
      this.dialogTitle = "新增菜单";
      this.category.parentCid = data.catId;
      this.category.catLevel = data.catLevel * 1 + 1;

      this.dialogVisible = true;
      //取消上次表单验证的效果
      //this.$refs["category"].resetFields();

      console.log(data);
    },
    //点击修改时
    edit(data) {
      this.dialogType = "edit";
      this.dialogTitle = "编辑菜单";
      this.category.catId = data.catId;

      this.$http({
        url: this.$http.adornUrl("/product/category/info/" + data.catId),
        method: "get",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.category.name = data.category.name;
          this.category.parentCid = data.category.parentCid;
          this.category.catLevel = data.category.catLevel;
          this.category.icon = data.category.icon;
          this.category.sort = data.category.sort;
          this.category.productUnit = data.category.productUnit;
          this.category.productCount = data.category.productCount;
        } else {
          this.$message.error(data.msg);
        }
      });

      this.dialogVisible = true;
      //取消上次表单验证的效果
      //this.$refs["category"].resetFields();

      console.log(data);
    },
    remove(node, data) {
      console.log(node);
      console.log(data);
      var ids = [data.catId];

      this.$confirm(`确定要删除` + data.name + `分类吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/product/category/delete"),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.getMenus();
              this.expandedKey = [node.parent.data.catId];
              this.$message({
                message: "菜单删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {},
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            message: "操作取消",
            type: "info",
            duration: 1500,
          });
        });
    },
    //是否可以被拖拽
    allowDrop(draggingNode, dropNode, type) {
      console.log("===================");
      console.log(draggingNode);
      console.log(dropNode);
      console.log(type);
      console.log("===================");

      //判断被拖拽的节点当前节点层数和不能大于等于3
      //查出层级 也就是说子类中最大的level
      this.maxLevel(draggingNode.data);
      //真正的深度  最里面的节点 层数 - 当前节点层数  +1
      let deep = this.maxNum - draggingNode.data.catLevel + 1;
      console.log(deep);
      //当前节点的层数 +  拖动后的节点 不等于大于4就ok
      if (type == "inner") {
        return deep + dropNode.level <= 3;
      } else {
        return deep + dropNode.parent.level <= 3;
      }

      // if (dropNode.data.label === '二级 3-1') {
      //   return type !== 'inner';
      // } else {
      //   return true;
      // }
    },
    maxLevel(node) {
      if (node.children != null && node.children.length > 0) {
        for (let i = 0; i < node.children.length; i++) {
          if (node.children[i].catLevel > this.maxNum) {
            this.maxNum = node.children[i].catLevel;
          }
          this.maxLevel(node.children[i]);
        }
      } else {
        //TODO
        if (node.level > this.maxNum) {
          this.maxNum = node.level;
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
</style>